package cn.toma.controller.sys;

import cn.toma.common.annotation.Log;
import cn.toma.common.exception.FfBootException;
import cn.toma.common.utils.StringUtils;
import cn.toma.common.vo.QueryRequest;
import cn.toma.common.vo.StringPool;
import cn.toma.config.web.utli.BaseController;
import cn.toma.dao.sys.LoginLogMapper;
import cn.toma.model.sys.Dept;
import cn.toma.model.sys.Dict;
import cn.toma.model.sys.LoginLog;
import cn.toma.model.sys.SysLog;
import cn.toma.service.sys.LogService;
import cn.toma.service.sys.LoginLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("log")
public class LogController extends BaseController
{

	private String message;

	@Autowired
	private LogService logService;

	@Autowired
	private LoginLogService loginLogService;
	@Resource
	private LoginLogMapper loginLogMapper;

	@GetMapping("/login")
	@RequiresPermissions("log:view")
	public Map<String, Object> logList(QueryRequest queryRequest, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, LoginLog sysLog)
	{
		Page<SysLog> objects = PageHelper.startPage(pageNum, pageSize);
		StringUtils.sortOrderBy(queryRequest, objects);
		loginLogService.find(sysLog);
		return getDataTable(objects.toPageInfo());
	}

	@GetMapping("/login/log")
	@RequiresPermissions("log:view")
	public List<LoginLog> logLoginList(String name)
	{
		Weekend<LoginLog> deptWeekend = new Weekend<>(LoginLog.class);
		WeekendCriteria<LoginLog, Object> weekendCriteria = deptWeekend.weekendCriteria();
		weekendCriteria.andEqualTo(LoginLog::getUsername, name);
		deptWeekend.orderBy("loginTime").desc();
		List<LoginLog> loginLogs = loginLogMapper.selectByCondition(deptWeekend);
		return loginLogs;
	}

	@GetMapping
	@RequiresPermissions("log:view")
	public Map<String, Object> logList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, SysLog sysLog)
	{
		Page<SysLog> objects = PageHelper.startPage(pageNum, pageSize);
		logService.findLogs(sysLog);
		return getDataTable(objects.toPageInfo());
	}

	@Log("删除登录系统日志")
	@DeleteMapping("/login/log/{ids}")
	@RequiresPermissions("log:delete")
	public void deleteLoginLogs(@NotBlank(message = "{required}") @PathVariable String ids) throws FfBootException
	{
		try {
			this.loginLogService.deleteByIds(ids);
		} catch (Exception e) {
			message = "删除登录系统日志失败";
			log.error(message, e);
			throw new FfBootException(message);
		}
	}

	@Log("删除系统日志")
	@DeleteMapping("/{ids}")
	@RequiresPermissions("log:delete")
	public void deleteLogss(@NotBlank(message = "{required}") @PathVariable String ids) throws FfBootException
	{
		try {
			String[] logIds = ids.split(StringPool.COMMA);
			this.logService.deleteLogs(logIds);
		} catch (Exception e) {
			message = "删除日志失败";
			log.error(message, e);
			throw new FfBootException(message);
		}
	}

//    @PostMapping("excel")
//    @RequiresPermissions("log:export")
//    public void export(QueryRequest request, SysLog sysLog, HttpServletResponse response) throws FfBootException {
//        try {
//            List<SysLog> sysLogs = this.logService.findLogs(request, sysLog).getRecords();
//            ExcelKit.$Export(SysLog.class, response).downXlsx(sysLogs, false);
//        } catch (Exception e) {
//            message = "导出Excel失败";
//            log.error(message, e);
//            throw new FfBootException(message);
//        }
//    }
}
